from rbac import models
from django import forms
from django.core.exceptions import ValidationError

class UserModelForm(forms.ModelForm):
    confirm_password = forms.CharField(label="确认密码",
                                       widget=forms.PasswordInput(),
                                       error_messages={'required': '确认密码不能为空'}
                                       )

    class Meta:
        model = models.UserInfo
        fields = ['name','email','password','confirm_password']
        widgets = {
            'name':forms.TextInput(),
            'email':forms.EmailInput(),
            'password':forms.PasswordInput(),
            'confirm_password':forms.PasswordInput(),
        }

        error_messages= {
            'name':{'required':'用户名不能为空'},
            'email':{'required':'邮箱不能为空'},
            'password':{'required':'密码不能为空'},
        }
    def __init__(self,*args,**kwargs):
        # 继承父类
        super().__init__(*args,**kwargs)
        # 循环出所有字段,统一给ModelForm字段加样式
        for name,field in self.fields.items():
            field.widget.attrs['class'] = 'form-control'


    def clean_confirm_password(self):
        '''
        检测密码是否一致
        :return:
        '''
        password = self.cleaned_data['password']
        confirm_password = self.cleaned_data['confirm_password']
        if password != confirm_password:
            raise ValidationError('两次密码不一致')
        return confirm_password


class UpdateUserModelForm(forms.ModelForm):
    class Meta:
        model = models.UserInfo
        fields = ['name','email']
        widgets = {
            'name':forms.TextInput(),
            'email':forms.EmailInput(),
        }

        error_messages= {
            'name':{'required':'用户名不能为空'},
            'email':{'required':'邮箱不能为空'},
        }
    def __init__(self,*args,**kwargs):
        # 继承父类
        super().__init__(*args,**kwargs)

        # 循环出所有字段,统一给ModelForm字段加样式
        for name,field in self.fields.items():
            field.widget.attrs['class'] = 'form-control'



class ResetPasswordUserModelForm(forms.ModelForm):
    confirm_password = forms.CharField(label="确认密码",
                                       widget=forms.PasswordInput(),
                                       error_messages={'required': '确认密码不能为空'}
                                       )
    class Meta:
        model = models.UserInfo
        fields = ['password','confirm_password']
        widgets = {
            'password':forms.PasswordInput(),
        }

        error_messages= {
            'password':{'required':'密码不能为空'},
        }

    def __init__(self,*args,**kwargs):
        # 继承父类
        super().__init__(*args,**kwargs)

        # 循环出所有字段,统一给ModelForm字段加样式
        for name,field in self.fields.items():
            field.widget.attrs['class'] = 'form-control'


    def clean_confirm_password(self):
        '''
        检测密码是否一致
        :return:
        '''
        password = self.cleaned_data['password']
        confirm_password = self.cleaned_data['confirm_password']
        if password != confirm_password:
            raise ValidationError('两次密码不一致')
        return confirm_password